package com.tenement.controller;

import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSONObject;
import com.tenement.common.Axios;
import com.tenement.enums.RespBusiness;
import com.tenement.service.GaoDeService;
import com.tenement.vo.GaoDeVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;


@RestController
@RequestMapping("gaode")
@RequiredArgsConstructor
@Api(tags = "高德接口")
public class GaodeController {

    private final GaoDeService gaoDeService;


    @ApiOperation("获取区域编码")
    @PostMapping("getAreaInfo")
    public Axios getAreaInfo(@RequestBody GaoDeVo gaoDeVo)
    {
        JSONObject areaInfo = gaoDeService.getAreaInfo(gaoDeVo);
        if (ObjectUtil.isNotNull(areaInfo)) {
            if(areaInfo.getString("status").equals("1")) {
                return Axios.success(JSONObject.parseObject(areaInfo.getString("regeocode")));
            }
            return Axios.error(500,areaInfo.getString("info"));
        }
        return Axios.error(RespBusiness.FAIL);
    }

    @ApiOperation("获取行政区域列表")
    @GetMapping("getBarrio")
    public Axios getBarrio(@RequestParam String keyWord)
    {
        JSONObject barrio = gaoDeService.getBarrio(keyWord);
        if (ObjectUtil.isNotNull(barrio)) {
            if(barrio.getString("status").equals("1")) {
                return Axios.success(barrio.getJSONArray("districts"));
            }
            return Axios.error(500,barrio.getString("info"));
        }
        return Axios.error(RespBusiness.FAIL);
    }

    @ApiOperation("获取天气信息")
    @PostMapping("getWeatherInfo")
    public Axios getWeatherInfo(@RequestBody GaoDeVo gaoDeVo)
    {
        JSONObject weatherInfo = gaoDeService.getWeatherInfo(gaoDeVo);
        if (ObjectUtil.isNotNull(weatherInfo)) {
            if(weatherInfo.getString("status").equals("1")) {
                if (gaoDeVo.getExtensions().equals("all")) {
                    return Axios.success(weatherInfo.getJSONArray("forecasts"));
                }
                return Axios.success(weatherInfo.get("lives"));
            }
            return Axios.error(500,weatherInfo.getString("info"));
       }
       return Axios.error(RespBusiness.FAIL);
   }

}
